/*******************************************************************************
 *  Copyright (c) 2010 Chase Technology Ltd.
 *  
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the GNU General Public License v3.0
 *  which is available at http://www.gnu.org/licenses/
 * 
 * Contributors:
 *     Doug Satchwell - initial API and implementation
 *******************************************************************************/
package org.jfunction.internal.parser;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.rules.ITokenScanner;
import org.jfunction.JFunctionFile;

/**
 * Abstract base class for parsing a section of a scenario file
 * 
 * @author Doug Satchwell
 */
public abstract class AbstractParser {

	protected final ITokenScanner scanner;
	protected IDocument document;

	public AbstractParser(ITokenScanner scanner) {
		this.scanner = scanner;
	}
	
	protected String getText() throws BadLocationException {
		return document.get(scanner.getTokenOffset(), scanner.getTokenLength());
	}

	public final void parse(IDocument document, ITypedRegion region, JFunctionFile jf) throws BadLocationException {
		this.document = document;
		scanner.setRange(document, region.getOffset(), region.getLength());
		doParse(jf);
	}

	protected abstract void doParse(JFunctionFile jf) throws BadLocationException;
}
